/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.sec.model.UrlResource;
import com.wubo.sec.service.IUrlResourceService;

/**
 * @author wubo
 * @CreateDate 2010-8-6
 * @version 1.0.01
 */
@Controller
public class UrlMgtAction extends BaseSecAction {
	@Autowired
	private IUrlResourceService urlResourceService;
	
	private List<UrlResource> urls;
	
	private UrlResource url;
	
	private String id;
	
	private String urlName;

	public IUrlResourceService getUrlResourceService() {
		return urlResourceService;
	}

	public void setUrlResourceService(IUrlResourceService urlResourceService) {
		this.urlResourceService = urlResourceService;
	}

	public List<UrlResource> getUrls() {
		return urls;
	}

	public void setUrls(List<UrlResource> urls) {
		this.urls = urls;
	}

	public UrlResource getUrl() {
		return url;
	}

	public void setUrl(UrlResource url) {
		this.url = url;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUrlName() {
		return urlName;
	}

	public void setUrlName(String urlName) {
		this.urlName = urlName;
	}

	public String list(){
		String where = "where 1=1";
		if(! StringUtils.isEmpty(urlName)){
			where = " where u.name like '%"+urlName+"%'";
		}
		total = urlResourceService.getTotal(where);
		
		urls = urlResourceService.getSplitPage(start, limit, where);
		
		return SUCCESS;
	}
	
	public String listForAssign(){
		urls = urlResourceService.findRunAs("");
		
		return "radio";
	}
	
	public String add(){
		try{
			urlResourceService.create(url);
			
			jsonSuccess(null);
		
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String edit(){
		try{
			urlResourceService.update(url);
	
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}

	public String del(){
		urlResourceService.delete(urlResourceService.get(id));
		
		list();
		
		flag = true;
		
		return SUCCESS;
	}
}
